﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace Client
{
    public partial class BanHang : Form
    {
        public CProduct sanpham ;
        public BanHang()
        {
            InitializeComponent();
        }

        private void btnclose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
        }

        private void BanHang_Load(object sender, EventArgs e)
        {
            
         
        }

        private void btnBan_Click(object sender, EventArgs e)
        {
            int soluong;
            bool flag = int.TryParse(txtSoLuong.Text, out soluong);
            if (flag == false)
            {
                MessageBox.Show("Số lượng phải kiểu int", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (soluong <= 0)
            {
                MessageBox.Show("Số lượng phải từ 1 trở lên", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            WriteLog(soluong);
            lbthongbao.Text = "Successed !!!";
            txtSoLuong.Text = "";
            MessageBox.Show("Success !!");
            this.Close();
        }
        private double ThanhTien(int soluong)
        {
            double thanhtien = 0;
            
            thanhtien = sanpham.Giatien * soluong;
            
            return thanhtien;
        }
        private void WriteLog(int soluong)
        {
            XmlDocument log = new XmlDocument();
            log.Load("log.xml");
            XmlElement root = log.DocumentElement;
            int id = sanpham.Ma;
            double thanhtien = ThanhTien( soluong);
            XmlElement node = log.CreateElement("Sanpham");
            node.SetAttribute("id", id.ToString());
            node.SetAttribute("soluong", soluong.ToString());
            node.SetAttribute("thanhtien", thanhtien.ToString());
            node.SetAttribute("ngayban", DateTime.Now.ToShortDateString());
            root.AppendChild(node);
            log.Save("log.xml");
            

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
        }
    }
}
